package com.xiong.xtools.twolistdown;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xiong.xtools.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : 熊文强
 * Time : 2017/10/9
 * Content : 下部展示列表
 */
public class TwoListDownActivity extends AppCompatActivity{
	private ListView allListView;
	List<AllInfo> allInfos = new ArrayList<>();
	private AllAdapter allAdapter;
	Boolean hindOrShow = false; // 显示或隐藏二级ListView

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tow_list_down);

		findView();
		initData();
		setAdapter();
	}

	private void setAdapter() {
		allAdapter = new AllAdapter(TwoListDownActivity.this);
		allListView.setAdapter(allAdapter);
	}

	private void initData() {
		String[] strings = {"FModule","1800M天线","4050386","900M天线","ACCU/DCCU"};
		for (int i = 0;i<5;i++){
			AllInfo allInfo = new AllInfo();
			allInfo.setAllName(strings[i]);
			allInfo.setAllCount(i+1);
			allInfo.setOK(true);

			List<AllDetailInfo> detailInfos = new ArrayList<>();
			for (int j = 0;j < i+1;j++){
				AllDetailInfo detailInfo = new AllDetailInfo();
				detailInfo.setDetailName(strings[i]);
				detailInfo.setDetailCode(j+"20TDR10G1000001");
				detailInfos.add(detailInfo);
			}
			allInfo.setDetailInfos(detailInfos);
			allInfos.add(allInfo);
		}
	}

	private void findView() {
		allListView = (ListView) findViewById(R.id.allListView);
	}


	class AllAdapter extends BaseAdapter {
		private Context context;
		private LayoutInflater inflater;

		public AllAdapter(Context context) {
			this.context = context;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return allInfos.size();
		}

		@Override
		public Object getItem(int i) {
			return allInfos.get(i);
		}

		@Override
		public long getItemId(int i) {
			return i;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			ViewHolder holder = null;
			AllInfo allInfo = allInfos.get(position);
			if (convertView == null){
				convertView = inflater.inflate(R.layout.all_item_layout,viewGroup,false);
				holder = new ViewHolder();
				holder.allItemLayout = (RelativeLayout) convertView.findViewById(R.id.allItemLayout);
				holder.allItemName = (TextView) convertView.findViewById(R.id.allItemName);
				holder.allItemCount = (TextView) convertView.findViewById(R.id.allItemCount);
				holder.allItemOK = (CheckBox) convertView.findViewById(R.id.allItemOK);
				holder.allItemOpen = (ImageView) convertView.findViewById(R.id.allItemOpen);
				holder.allItemListView = (ListView) convertView.findViewById(R.id.allItemListView);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			final ListView lv = holder.allItemListView;
			final List<AllDetailInfo> detailInfos = allInfo.getDetailInfos();
			holder.allItemName.setText(allInfo.getAllName());
			holder.allItemCount.setText("数量:"+allInfo.getAllCount());

			if (hindOrShow){
				bindExpandList(detailInfos,lv);
			}

			final ViewHolder fHolder = holder;
			holder.allItemLayout.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					if (lv.getVisibility() == View.VISIBLE) {
						lv.setVisibility(View.GONE);
						fHolder.allItemOpen.setImageResource(R.mipmap.ic_all_open);
						hindOrShow = false;
					} else {
						lv.setVisibility(View.VISIBLE);
						fHolder.allItemOpen.setImageResource(R.mipmap.ic_all_close);
						hindOrShow = true;
						bindExpandList(detailInfos, lv);
					}
				}
			});

			return convertView;
		}
	}
	static class ViewHolder{
		RelativeLayout allItemLayout;
		TextView allItemName;
		TextView allItemCount;
		CheckBox allItemOK;
		ImageView allItemOpen;
		ListView allItemListView;
	}

	private void bindExpandList(List<AllDetailInfo> detailInfos, ListView lv){
		if (detailInfos == null || detailInfos.size() == 0){
			return;
		}
		AllDetailAdapter detailAdapter = new AllDetailAdapter(TwoListDownActivity.this,detailInfos);
		detailAdapter.notifyDataSetChanged();
		lv.setAdapter(detailAdapter);
		setListViewHeightBasedOnChildren(lv);
	}

	class AllDetailAdapter extends BaseAdapter {
		private Context context;
		private List<AllDetailInfo> detailInfos;
		private LayoutInflater inflater;

		public AllDetailAdapter(Context context, List<AllDetailInfo> detailInfos) {
			this.context = context;
			this.detailInfos = detailInfos;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return detailInfos.size();
		}

		@Override
		public Object getItem(int i) {
			return detailInfos.get(i);
		}

		@Override
		public long getItemId(int i) {
			return i;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			ViewHolder1 holder1 = null;
			AllDetailInfo detailInfo = detailInfos.get(position);
			if (convertView == null){
				convertView = inflater.inflate(R.layout.all_item_detail,null);
				holder1 = new ViewHolder1();
				holder1.allDetailName = (TextView) convertView.findViewById(R.id.allDetailName);
				holder1.allDetailCode = (TextView) convertView.findViewById(R.id.allDetailCode);
				holder1.allDetailScan = (TextView) convertView.findViewById(R.id.allDetailScan);
				convertView.setTag(holder1);
			}else {
				holder1 = (ViewHolder1) convertView.getTag();
			}
			holder1.allDetailName.setText(detailInfo.getDetailName());
			holder1.allDetailCode.setText(detailInfo.getDetailCode());

			holder1.allDetailScan.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Toast.makeText(context,"点击后跳转去扫码，添加对应的串码", Toast.LENGTH_SHORT).show();
				}
			});

			return convertView;
		}
	}
	static class ViewHolder1{
		TextView allDetailName;
		TextView allDetailCode;
		TextView allDetailScan;
	}


	public void setListViewHeightBasedOnChildren(ListView listView) {
		// 获取ListView对应的Adapter
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
			// listAdapter.getCount()返回数据项的数目
			View listItem = listAdapter.getView(i, null, listView);
			// 计算子项View 的宽高
			listItem.measure(0, 0);
			// 统计所有子项的总高度
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}


}
